<template>
    <transition name="fade"
      @after-leave='afterLeave'
    >
        <div class="toast-shade" v-show="visible">
            <div class="toast" >
                <img class="icon" v-if="icon==='success'" src="../assets/images/success.png">
                <img class="icon" v-if="icon==='error'" src="../assets/images/error.png">
                <p>{{content}}</p>
            </div>
        </div>
    </transition>
</template>
<script>
export default {
  data() {
      return {
          visible: false,
          autoCloseTime: 3000
      }
  },
  props: {
    icon: {
      type: String,
      default: "",
    },
    content: {
      type: String,
      default: "",
    }
  },
  mounted() {
      this.creatTimer()
  },
  beforeDestroy() {
      this.clearTimer()
  },
  methods: {
      creatTimer() {
          if (this.autoCloseTime) {
              this.timer = setTimeout(() => {
                  this.visible = false
              }, this.autoCloseTime)
          }
      },
      clearTimer() {
          if (this.timer) {
            clearTimeout(this.timer)
          }
      },
      afterLeave() {
          this.$emit('close')
      }
  }
};
</script>
<style lang="less" scoped>
.toast-shade{
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0
}
.toast{
    width: 50%;
    padding: 0.8rem 3%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background: rgba(0,0,0,.7);
    border-radius: 5px;
    .icon{
        width: 2rem;
        height: 2rem;
        display: block;
        margin: 0 auto 1rem auto;
    }
    p{
        text-align: center;
        color: #ffffff
    }
}
</style>

